<?php
require_once 'constants.php';

abstract class Buffalo_Meta_Box {

    private $id;
    private $title;
    private $page;

    function __construct($id, $title, $page) {
        $this->id = $id;
        $this->title = $title;
        $this->page = $page;

        add_action('add_meta_boxes', array(&$this, 'add_meta_box'));
        add_action('save_post', array(&$this, 'save_post_data'));

        if (is_admin()) {
            add_action('admin_init', array(&$this, 'register_meta_box'));
        }
    }

    abstract function render_content($post);

    abstract function save_post_data($post_id);

    function add_meta_box() {
        $callback = array(&$this, 'render_content');
        add_meta_box($this->id, $this->title, $callback, $this->page);
    }

    function register_meta_box() {
        return;
    }

}

class Price_Meta_Box extends Buffalo_Meta_Box {

    function __construct() {
        parent::__construct('buffalo-price', __('Price'), PRODUCT_POST_TYPE);
    }

    function render_content($post) {
        $price = get_post_meta($post->ID, PRICE_META_KEY, true);
        echo '<input type="text" class="widefat" name="price" value="' . $price . '" />';
    }

    function save_post_data($post_id) {
        if ($_POST['post_type'] == PRODUCT_POST_TYPE) {
            $price = $_POST['price'];
            if (!empty($price)) {
                update_post_meta($post_id, PRICE_META_KEY, $price);
            }
        }
    }

    function register_meta_box() {
        return new Price_Meta_Box();
    }

}

class Customer_Meta_Box extends Buffalo_Meta_Box {

    public function __construct() {
        parent::__construct('buffalo-customer', __('Customer'), ORDER_POST_TYPE);
    }

    public function render_content($post) {
        $customer_name = get_post_meta($post->ID, 'customer_name', true);
        $customer_phone = get_post_meta($post->ID, 'customer_phone', true);
        $shipping_address = get_post_meta($post->ID, 'shipping_address', true);
        ?>
        <div class="input-text-wrap">
            <label for="customername">Name</label>
            <input id="customername" type="text" name="customer-name" class="widefat" value="<?php echo $customer_name; ?>" />
        </div>
        <div class="input-text-wrap">
            <label for="customerphone">Phone</label>
            <input id="customerphone" type="text" name="customer-phone" class="widefat" value="<?php echo $customer_phone; ?>" />
        </div>
        <div class="input-text-wrap">
            <label for="shipping-address">Shipping Address</label>
            <input id="shipping-address" type="text" name="shipping-address" class="widefat" value="<?php echo $shipping_address; ?>" />
        </div>
        <?php
    }

    public function save_post_data($post_id) {
        if ($_POST['post_type'] == ORDER_POST_TYPE) {
            $customer_name = $_POST['customer-name'];
            $customer_phone = $_POST['customer-phone'];
            $shipping_address = $_POST['shipping-address'];
            update_post_meta($post_id, 'customer_name', $customer_name);
            update_post_meta($post_id, 'customer_phone', $customer_phone);
            update_post_meta($post_id, 'shipping_address', $shipping_address);
        }
    }

    public function register_meta_box() {
        return new Customer_Meta_Box();
    }

}

class Products_Meta_Box extends Buffalo_Meta_Box {

    public function __construct() {
        parent::__construct('buffalo-products', __('Products'), ORDER_POST_TYPE);
    }

    public function render_content($post) {
        $data = unserialize(get_post_meta($post->ID, 'buffalo-products', true));
        ?>
        <table class="widefat">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Price</th>
                    <th>Quantity</th>
                    <th>Total Price</th>
                </tr>
            </thead>
            <tfoot>
                <tr>
                    <th>Name</th>
                    <th>Price</th>
                    <th>Quantity</th>
                    <th>Total Price</th>
                </tr>
            </tfoot>
            <tbody>
                <?php
                $subtotal = 0;
                if (is_array($data)) {
                    foreach ($data as $item) {
                        ?>
                        <tr>
                            <td><?php echo $item['name']; ?></td>
                            <td><?php echo '$' . $item['price']; ?></td>
                            <td><?php echo $item['quantity']; ?></td>
                            <td><?php echo '$' . $item['total_price']; ?></td>
                        </tr>
                        <?php
                        $subtotal += $item['total_price'];
                    }
                }
                ?>
            </tbody>
        </table>
        <p>Subtotal: <?php echo '$' . $subtotal; ?></p>
        <?php
    }

    public function save_post_data($post_id) {
        if ($_POST['post_type'] == ORDER_POST_TYPE) {
            // TODO: Do nothing!
        }
    }

    public function register_meta_box() {
        return new Products_Meta_Box;
    }

}

class Status_Meta_Box extends Buffalo_Meta_Box {

    public function __construct() {
        parent::__construct('buffalo-status', __('Status'), ORDER_POST_TYPE);
    }

    public function render_content($post) {
        $status = get_post_meta($post->ID, 'buffalo-status', true);

        switch ($status) {
            case 'Pending':
                ?>
                <input type="radio" name="status" value="Pending" checked />
                Pending <br />
                <input type="radio" name="status" value="Shipped" />
                Shipped <br />
                <?php
                break;
            case 'Shipped':
                ?>
                <input type="radio" name="status" value="Pending" />
                Pending <br />
                <input type="radio" name="status" value="Shipped" checked />
                Shipped <br />
                <?php
                break;
            default:
                ?>
                <input type="radio" name="status" value="Pending" checked />
                Pending <br />
                <input type="radio" name="status" value="Shipped" />
                Shipped <br />
                <?php
                break;
        }
    }

    public function save_post_data($post_id) {
        if ($_POST['post_type'] == ORDER_POST_TYPE) {
            $status = $_POST['status'];
            if (empty($status)) {
                $status = 'Pending';
            }

            update_post_meta($post_id, 'buffalo-status', $status);
        }
    }

    public function register_meta_box() {
        return new Status_Meta_Box();
    }

}
?>
